﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RuoVea.ExConfig;
using RuoVea.ExDto;
using RuoVea.ExDto.Language;
using RuoVea.ExEnum;
using RuoVea.ExIdGen;
using RuoVea.ExSugar;
using RuoVea.ExUtil;
using RuoVea.H5DS.Dto;
using RuoVea.H5DS.Models;

namespace RuoVea.H5DS.Controllers;

[Authorize(Roles = "None")]
public partial class H5DSController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly SugarRepository<H5Template> _thisRepository;
    private readonly SugarRepository<H5ServerInfo> _serverInfoRepository;
    private readonly ICurrentUser _currentUser;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="thisRepository"></param>
    /// <param name="currentUser"></param>
    /// <param name="serverInfoRepository"></param>
    public H5DSController(SugarRepository<H5Template> thisRepository, ICurrentUser currentUser, SugarRepository<H5ServerInfo> serverInfoRepository)
    {
        _thisRepository = thisRepository;
        _currentUser = currentUser;
        _serverInfoRepository = serverInfoRepository;
    }

    public IActionResult Index(string appId)
    {
        ViewBag.appId = appId;
        return View();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("H5DS/data/{id:long}")]
    public async Task<RestfulResult<H5Template>> GetDataAsync(long id)
    {
        RestfulResult<H5Template> restful = new() { Code = CodeStatus.BadRequest };

        if (id == 0)
        {
            restful.Message = "失败";
            return restful;
        }
        H5Template data = await _thisRepository.GetFirstAsync(x => x.Id == id);
        data.ResultContent = "";
        if (data.Content.IsNullOrWhiteSpace())
        {
            var defultData = (await _serverInfoRepository.GetFirstAsync(x => x.Code == "defultData"))?.Content;
            if (defultData.IsNullOrWhiteSpace())
            {
                data.Content = AppSettings.GetConfig<object>("H5Options:defultData")?.Json2Str();
                if (data.Content.NotNullOrWhiteSpace())
                   await _serverInfoRepository.InsertAsync(new H5ServerInfo { Id = IdGenerator.Id, Code = "defultData", Content = data.Content, CreateTime = DateTime.Now, IsDelete = 0 });
            }
            else
            {
                data.Content = defultData;
            }
        }
        restful.Data = data;
        restful.Code = CodeStatus.OK;
        return restful;
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("H5DS/submit")]
    public async Task<RestfulResult> SubmitAsync([FromBody] H5Template data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };

        var isExist = await _thisRepository.IsAnyAsync(u => u.Id == data.Id /*&& u.Creator == _currentUser.UserId*/);
        if (!isExist)
        {
            restful.Message = ErrorEnum.D3000.GetDescription(i18n.ResourceManager);
            return restful;
        }
        bool result = await _thisRepository.UpdateAsync(data);
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "修改失败";
            return restful;
        }
    }
}

